#include <stdio.h>  // for printf
#include <string.h> // for memory operation
#include <stdlib.h>
#include <unistd.h>  // for sleep
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "lwip/netifapi.h"
#include "lwip/api_shell.h"
#include "lwip/sockets.h"
#include "wifi_sta_connect.h"
#include "cJSON.h"

#define AP_SSID "PPPP"
#define AP_PWD  "12345678"

void sta_test_entry(void *arg)
{
    printf("start sta test!!\n");
    WifiConnect(AP_SSID,AP_PWD);

        /****************以下为UDP服务器代码,默认IP:192.168.5.1***************/
	//在sock_fd 进行监听
    int sock_fd;
    //服务端地址信息
	struct sockaddr_in server_sock;

    //创建socket
    //AF_INET:IPv4协议，SOCK_DGRAM:数据报
	if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
	{
		perror("socket is error.\r\n");
		return -1;
	}

     printf("=== > sock_fd=%d\n",sock_fd);
    const int opt=-1;

    int ret;
    char recvBuf[128] = {0};
    // char sendBuff[128] = {0};
    char payload[400]={0};
    //客户端地址信息
    struct sockaddr_in send_addr;
    int size_send_addr= sizeof(struct sockaddr_in);
    send_addr.sin_family = AF_INET;
    send_addr.sin_addr.s_addr = inet_addr("172.29.248.1");
    send_addr.sin_port = htons(3861);

    int count = 0;

    while (1)
    {    
        memset(recvBuf, 0, sizeof(recvBuf));

        // bzero(recvBuf, sizeof(recvBuf));
        cJSON *root = cJSON_CreateObject();
        if(root != NULL){
            cJSON_AddStringToObject(root, "deviceId", "air001");
            cJSON *pro_obj = cJSON_CreateObject();
            cJSON_AddNumberToObject(pro_obj, "PM25", 35);
            cJSON_AddNumberToObject(pro_obj, "Temperature", 30);
            cJSON_AddNumberToObject(pro_obj, "Humidity", 60);
            cJSON_AddNumberToObject(pro_obj, "RestFilter", 20);
            cJSON_AddNumberToObject(pro_obj, "WorkStatus", 0);
            cJSON_AddNumberToObject(pro_obj, "ModeStatus", 0);
            cJSON_AddItemToObject(root, "properties", pro_obj);

            char *payload_str = cJSON_PrintUnformatted(root);

            strcpy(payload, payload_str);

            cJSON_free(payload_str);
            cJSON_Delete(root);
        }
        //格式化数据
        // sprintf_s(payload,sizeof(payload),"%s ++%d\n",payload,count);
        printf("send data:%s\r\n",payload);

        //发送数据
        ret = sendto(sock_fd, payload, strlen(payload), 0, 
        (struct sockaddr *)&send_addr, (socklen_t)size_send_addr);
        if (ret < 0)
        {
            printf("UDP server send failed!\r\n");
            // return -1;
        }

           //打印地址时传指针
        ret = recvfrom(sock_fd, recvBuf, sizeof(recvBuf), 0, 
        (struct sockaddr *)&send_addr, (socklen_t*)&size_send_addr);
        if (ret < 0)
        {
            printf("UDP recv failed!! ret = %d\r\n",ret);
            // return -1;
        }
        printf("recv:%s\n",recvBuf);

        count += 1;
        // printf("recv:%s , ip_addr:%s\n",recvBuf,inet_ntoa(send_addr.sin_addr).count++);
        // sprintf_s(sendBuff,sizeof(sendBuff),"%s ++%d\n",recvBuf,count);
        // sleep(5);
    }
    /*********************END********************/
}

void sta_test_main()
{
    osThreadAttr_t attr;

    attr.name = "sta_test";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 10240;
    attr.priority = 25;

    if (osThreadNew((osThreadFunc_t)sta_test_entry, NULL, &attr) == NULL)
    {
        printf("Falied to create wifi sta!!\r\n");
    }
}

APP_FEATURE_INIT(sta_test_main);